/*
 * Copyright (C) 2014 Francisco José Morero Peyrona. All Rights Reserved.
 *
 * This file is part of JStatViewer project: https://code.google.com/p/jstat-viewer/
 *
 * GNU Classpath is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the free
 * Software Foundation; either version 3, or (at your option) any later version.
 *
 * Tapas is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Tapas; see the file COPYING.  If not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

package jstatviewer.reader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import jstatviewer.Util;

/**
 * Read and parse files generated by JStat tool.
 *
 * @see http://docs.oracle.com/javase/1.5.0/docs/tooldocs/share/jstat.html
 * @author peyrona
 */
public class JStatFileReader
{
    private final BufferedReader reader;

    //----------------------------------------------------------------------------//

    /**
     *
     * @param fLog
     * @throws java.io.FileNotFoundException
     */
    public JStatFileReader( File fLog ) throws FileNotFoundException
    {
        this.reader = new BufferedReader( new FileReader( fLog ) );
    }

    //----------------------------------------------------------------------------//

    /**
     * Reads and analize all lines in the file one by one.
     * 
     * @return
     * @throws java.io.IOException
     */
    public JStats parse() throws IOException
    {
        JStats stats = new JStats();
        String line;

        while( (line = reader.readLine()) != null )
        {
            line = line.trim();

            if( line.length() > 0 )
            {
                if( isDataLine( line ) )    // It is a line with data
                {
                    List<Double> lstValues = createColumnValues( line );

                    for( int nColIdx = 0; nColIdx < lstValues.size(); nColIdx++ )
                    {
                        stats.add( nColIdx, lstValues.get( nColIdx ) );
                    }
                }
                else                       // Should be columns header
                {
                    stats.setColumnNames( createColumnNames( line ) );
                }
            }
        }

        return stats;
    }

    //----------------------------------------------------------------------------//

    private String[] createColumnNames( String line )
    {
        String[]         arr   = line.split( " " );
        List<String>     lst   = new LinkedList<>( Arrays.asList( arr ) );
        Iterator<String> itera = lst.iterator();

        // Remove empty elements
        while( itera.hasNext() )
        {
            if( Util.isEmpty( itera.next() ) )
            {
                itera.remove();
            }
        }

        return lst.toArray( new String[0] );
    }

    private List<Double> createColumnValues( String line )
    {
        String[]     asValues  = line.split( " " );
        List<Double> lstValues = new ArrayList<>( asValues.length );

        for( String asValue : asValues )
        {
            if( Util.isNotEmpty( asValue ) )
            {
                try
                {
                    lstValues.add( Double.parseDouble( asValue ) );
                }
                catch( NumberFormatException nfe )
                {
                    lstValues.add( Double.NaN );
                }
            }
        }

        return lstValues;
    }

    private boolean isDataLine( String line )
    {
        return line.charAt( 0 ) >= '0' &&
               line.charAt( 0 ) <= '9';
    }
}